package com.example.Test;

import com.example.Dao.DormitoryDAO;
import com.example.Dao.StudentDAO;
import com.example.Dao.UserDAO;
import com.example.pojo.Dormitory;
import com.example.pojo.Student;
import com.example.pojo.User;

import java.util.Scanner;

/**
 * ClassName: Main
 * Package: com.example.Test
 * Description:
 *
 * @Author: liu
 * @Create: 2025/5/28 9:37
 * @Version:1.0
 */

public class Main {
    private static Scanner scanner = new Scanner(System.in);
    private static UserDAO userDao = new UserDAO();
    private static StudentDAO studentDao = new StudentDAO();
    private static DormitoryDAO dormDao = new DormitoryDAO();
    public static void main(String[] args) {
        while (true) {
            System.out.println("=== 宿舍管理系统 ===");
            System.out.println("1. 管理员登录");
            System.out.println("2. 学生登录");
            System.out.println("3. 退出");
            System.out.println("请输入：");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 处理换行符

            switch (choice) {
                case 1:
                    adminLogin();
                    break;
                case 2:
                    studentLogin();
                    break;
                case 3:
                    System.out.println("系统退出");
                    return;
                default:
                    System.out.println("无效选择");
            }
        }
    }

    // 管理员登录逻辑
    private static void adminLogin() {
        System.out.print("管理员ID：");
        String id = scanner.nextLine();
        System.out.print("密码：");
        String password = scanner.nextLine();
        User user = userDao.login(id, password);
        if (user != null && "admin".equals(user.getRole())) { // 假设role存储在role中
            adminMenu();
        } else {
            System.out.println("登录失败：账号或密码错误");
        }
    }

    // 学生登录逻辑
    private static void studentLogin() {
        System.out.print("学生ID：");
        String id = scanner.nextLine();
        System.out.print("密码：");
        String password = scanner.nextLine();
        User user = userDao.login(id, password);
        if (user != null && "student".equals(user.getRole())) { // 假设role存储在employeename中
            studentMenu();
        } else {
            System.out.println("登录失败：账号或密码错误");
        }
    }

    // 管理员菜单
    private static void adminMenu() {
        while (true) {
            System.out.println("=== 管理员菜单 ===");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 添加宿舍");
            System.out.println("4. 删除宿舍");
            System.out.println("5. 查看学生列表");
            System.out.println("6. 查看宿舍列表");
            System.out.println("7. 修改宿舍信息");
            System.out.println("8. 查看宿舍人数");
            System.out.println("9. 退出");
            System.out.println("请输入：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    addDormitory();
                    break;
                case 4:
                    deleteDormitory();
                    break;
                case 5:
                    studentDao.listStudents();
                    break;
                case 6:
                    dormDao.listDormitories();
                    break;
                case 7:
                    updateDormitory();
                    break;
                case 8:
                    dormDao.getDormitoryOccupancy();
                    break;
                case 9:
                    return;
                default:
                    System.out.println("无效选择");
            }
        }
    }

    // 学生菜单
    private static void studentMenu() {
        while (true) {
            System.out.println("=== 学生菜单 ===");
            System.out.println("1. 查看宿舍列表");
            System.out.println("2. 查看宿舍人数");
            System.out.println("3. 查看单个宿舍人数");
            System.out.println("4. 退出");
            System.out.println("请输入：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    dormDao.listDormitories();
                    break;
                case 2:
                    dormDao.getDormitoryOccupancy();
                    break;
                case 3:
                    viewSingleDormitoryOccupancy();
                    break;
                case 4:
                    return;
                default:
                    System.out.println("无效选择");
            }
        }
    }

    // 添加学生（管理员操作）
    private static void addStudent() {
        System.out.print("学生ID：");
        String id = scanner.nextLine();
        System.out.print("姓名：");
        String name = scanner.nextLine();
        System.out.print("密码：");
        String password = scanner.nextLine();
        System.out.print("宿舍ID：");
        String dormId = scanner.nextLine();
        Student student = new Student(id, name, password, dormId);
        studentDao.addStudent(student);
    }

    // 添加宿舍（管理员操作）
    private static void addDormitory() {
        System.out.print("宿舍ID：");
        String id = scanner.nextLine();
        System.out.print("宿舍名称：");
        String name = scanner.nextLine();
        Dormitory dorm = new Dormitory(id, name);
        dormDao.addDormitory(dorm);
    }
    // 修改宿舍信息（管理员操作）
    private static void updateDormitory() {
        System.out.print("要修改的宿舍ID：");
        String id = scanner.nextLine();
        System.out.print("新的宿舍名称：");
        String name = scanner.nextLine();
        dormDao.updateDormitory(id, name); // 直接传递ID和新名称
    }

    // 删除宿舍（管理员操作）
    private static void deleteDormitory() {
        System.out.print("要删除的宿舍ID：");
        String dormitoryId = scanner.nextLine();
        dormDao.deleteDormitory(dormitoryId);
    }
    // 查看单个宿舍人数的方法
    private static void viewSingleDormitoryOccupancy() {
        System.out.print("请输入要查询的宿舍ID：");
        String dormitoryId = scanner.nextLine();
        dormDao.getSingleDormitoryOccupancy(dormitoryId);
    }
    //删除学生
    private static void deleteStudent() {
        System.out.print("请输入要删除的学生ID：");
        String studentId = scanner.nextLine();
        studentDao.deleteStudent(studentId);
        System.out.println("学生删除成功");
    }

}